package main

import (
	"gin_oj/helper"
	"gin_oj/model"
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB = Init()

func Init() *gorm.DB {
	dsn := "root:@tcp(127.0.0.1:3306)/gin_oj?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Panic("database connect error: ", err)
	}
	db.AutoMigrate(&model.ProblemBasic{}, &model.CategoryBasic{}, &model.ProblemCategory{},
		&model.UserBasic{}, &model.SubmitBasic{})
	return db
}

func insertProblem() {
	re1 := model.ProblemBasic{
		Identity:   "problem_1",
		Title:      "problem_1",
		Content:    "问题1正文",
		MaxRuntime: 3000,
		MaxMem:     3000,
	}
	re2 := model.ProblemBasic{
		Identity:   "problem_2",
		Title:      "problem_2",
		Content:    "问题2正文",
		MaxRuntime: 3000,
		MaxMem:     3000,
	}
	re3 := model.ProblemBasic{
		Identity:   "problem_3",
		Title:      "problem_3",
		Content:    "问题3正文",
		MaxRuntime: 3000,
		MaxMem:     3000,
	}
	DB.Model(&model.ProblemBasic{}).Create(&re1)
	DB.Model(&model.ProblemBasic{}).Create(&re2)
	DB.Model(&model.ProblemBasic{}).Create(&re3)
}

func insertCategory() {
	re1 := model.CategoryBasic{
		Identity: "category_1",
		Name:     "类型1",
		ParentId: 1,
	}
	re2 := model.CategoryBasic{
		Identity: "category_2",
		Name:     "类型2",
		ParentId: 1,
	}
	re3 := model.CategoryBasic{
		Identity: "category_3",
		Name:     "类型3",
		ParentId: 1,
	}
	DB.Model(&model.CategoryBasic{}).Create(&re1)
	DB.Model(&model.CategoryBasic{}).Create(&re2)
	DB.Model(&model.CategoryBasic{}).Create(&re3)
}

func insertPC() {
	re1 := model.ProblemCategory{
		ProblemId:  "problem_1",
		CategoryId: "category_2",
	}
	re2 := model.ProblemCategory{
		ProblemId:  "problem_2",
		CategoryId: "category_1",
	}
	re3 := model.ProblemCategory{
		ProblemId:  "problem_3",
		CategoryId: "category_3",
	}
	DB.Model(&model.ProblemCategory{}).Create(&re1)
	DB.Model(&model.ProblemCategory{}).Create(&re2)
	DB.Model(&model.ProblemCategory{}).Create(&re3)
}

func insertUser() {
	re1 := model.UserBasic{
		Identity: "user_1",
		Name:     "用户1",
		Password: helper.GetMd5("123456"),
		Phone:    "123",
		Mail:     "user1@qq.com",
	}
	re2 := model.UserBasic{
		Identity: "user_2",
		Name:     "用户2",
		Password: helper.GetMd5("123456"),
		Phone:    "123",
		Mail:     "user2@qq.com",
	}
	re3 := model.UserBasic{
		Identity: "user_3",
		Name:     "用户3",
		Password: helper.GetMd5("123456"),
		Phone:    "123",
		Mail:     "user3@qq.com",
	}
	DB.Model(&model.UserBasic{}).Create(&re1)
	DB.Model(&model.UserBasic{}).Create(&re2)
	DB.Model(&model.UserBasic{}).Create(&re3)
}

func insertSubmit() {
	re1 := model.SubmitBasic{
		Identity:  "sumbit_1",
		UserId:    "user_1",
		ProblemId: "problem_1",
		Path:      "/usr/bin/sub1",
		Status:    1,
	}
	re2 := model.SubmitBasic{
		Identity:  "sumbit_2",
		UserId:    "user_2",
		ProblemId: "problem_2",
		Path:      "/usr/bin/sub2",
		Status:    2,
	}
	re3 := model.SubmitBasic{
		Identity:  "sumbit_3",
		UserId:    "user_3",
		ProblemId: "problem_3",
		Path:      "/usr/bin/sub3",
		Status:    3,
	}
	DB.Model(&model.SubmitBasic{}).Create(&re1)
	DB.Model(&model.SubmitBasic{}).Create(&re2)
	DB.Model(&model.SubmitBasic{}).Create(&re3)
}

func main() {
	insertProblem()
	insertCategory()
	insertPC()
	insertUser()
	insertSubmit()
}
